package com.bankcomm.ui;

import java.util.Map;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bankcomm.BankcommApplication;
import com.bankcomm.R;
import com.bankcomm.common.LPMid;
import com.bankcomm.util.LPUtils;
import com.rytong.tools.datastorage.PackageManagerAndroid;
import com.rytong.tools.ui.BaseView;
import com.rytong.tools.utils.LPToastUtil;
import com.rytong.tools.utils.Utils;

/**
 * BOCPushMessage.java
 * @author cheng.li 2013-5-21 上午11:09:04
 */
public class BOCPushMessage extends BaseViewCopy implements OnClickListener{

	private Button btnGoHome;
	private ImageButton info;
	private ListView listView;
	private ImageView switchOnOff;
	private boolean isOn = true;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.boc_push_message);

		String pushMessage = PackageManagerAndroid.ANDROIDDB.get("pushMessage");
		if(null == pushMessage || pushMessage.equalsIgnoreCase("false")
				|| "".equalsIgnoreCase(pushMessage)){
			isOn = false;
		}else{
			isOn = true;
		}
		LPMid.isPushActivity = true;
		init();
		System.gc();
	}

	public static void scaledTHeight(LinearLayout view){
    	if(null == view)
    		return;
    	RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,Utils.getScaledValueY(LPUtils.TITLE_HEIGHT));
    	view.setLayoutParams(lp);
	}
	
	private void init() {
		// TODO Auto-generated method stub
		scaledTHeight((LinearLayout)findViewById(R.id.top));
		
		//进入主菜单按钮
		btnGoHome = (Button)findViewById(R.id.button_go_home);
		//info按钮
		info = (ImageButton)findViewById(R.id.titleRight);
		LPUtils.setTitleButtonWH(info);
		//消息列表view
		listView = (ListView)findViewById(R.id.listview);
		//推送通知开关
		switchOnOff = (ImageView)findViewById(R.id.switch_on_off);
		
		changeSwitch();

		listView.setScrollingCacheEnabled(false);
		listView.setVerticalFadingEdgeEnabled(false);
		listView.setVerticalScrollBarEnabled(true);
		listView.setSelector(R.drawable.list_item_bg);
		listView.setDivider(this.getResources().getDrawable(R.drawable.horizontal_line));
		listView.setDividerHeight(2);
		
		listView.setAdapter(new NewsAdapter());
		
		btnGoHome.setOnClickListener(this);
		info.setOnClickListener(this);
		switchOnOff.setOnClickListener(this);
		
		listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Map<String, Object> map = LPMid.getInstance().um_.data.pushNews.get(arg2);
				String url = (String) map.get("url");
				if(null != url && !"".equalsIgnoreCase(url)){
					LPMid.getInstance().um_.gotoEmpViewFromHome(BOCPushMessage.this, url,"");
				}
			}
		});
	}

	/**
	 * 改变推送通知开关按钮
	 * */
	private void changeSwitch(){
		if(isOn){
			switchOnOff.setImageResource(R.drawable.switch_yes);
		}else{
			switchOnOff.setImageResource(R.drawable.switch_no);
		}
	}
	
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch(view.getId()){
		case R.id.button_go_home:
			gotoActivity(this);
			break;
		case R.id.titleRight:
			LPToastUtil.showAlertMessage(this,"e动交行"+getClientVersion()+"正式版\n©交通银行版权所有");
			break;
		case R.id.switch_on_off:
			isOn = isOn? false:true;
			//存储是否开启推送消息页面
			if(isOn){
				PackageManagerAndroid.ANDROIDDB.save("pushMessage", "true");
			}else{
				PackageManagerAndroid.ANDROIDDB.save("pushMessage", "false");
			}
			changeSwitch();
			break;
		}
	}

	/**
	 * 获取客户端版本号
	 * */
	public String getClientVersion() {
		PackageInfo packageInfo = null;
		try {
			packageInfo = getPackageManager().getPackageInfo(this.getPackageName(), PackageManager.GET_CONFIGURATIONS);
			return packageInfo.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 跳转到首页
	 * @param activity
	 */
	public static void gotoActivity(final Activity activity){
//		final WaitProgressDialog progressDialog = new WaitProgressDialog(activity,"加载首页...");
//		progressDialog.dialog_.show();
//		
//		Runnable runnable = new Runnable() {
//			@Override
//			public void run() {
//				Intent intent = new Intent();
//				intent.setClass(activity, BOCMainHome.class);
//				activity.startActivity(intent);
//	    		BankcommApplication.getInstance().removeActivity(activity);
//	    		progressDialog.dismiss();
//	    		activity.finish();   
//			}
//		};
//		new Handler().postDelayed(runnable, 100);
		if(activity instanceof EMPView && null != activityList){
			for(Activity act:BaseView.activityList){
				if(null != act && act instanceof BOCPushMessage){
					act.finish();
					break;
				}
			}
		}
		activity.finish();   
		BankcommApplication.getInstance().removeActivity(activity);
	}

	private class NewsAdapter extends BaseAdapter{

		private LayoutInflater flater;
		private NewsAdapter(){
			if(null == flater){
				flater = LayoutInflater.from(BOCPushMessage.this);
			}
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return LPMid.getInstance().um_.data.pushNews.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return LPMid.getInstance().um_.data.pushNews.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null) { 
	            convertView = flater.inflate(R.layout.layout_push_message_item, null); 
	            holder = new ViewHolder();
	            holder.newsItem = (TextView)convertView.findViewById(R.id.item);
	            convertView.setTag(holder);
	        } else{
	        	holder = (ViewHolder) convertView.getTag();
	        }
			
			Map<String,Object> map = LPMid.getInstance().um_.data.pushNews.get(arg0);
			String content = (String) map.get("content");
			holder.newsItem.setText(content != null ? content:"");
			
			return convertView;
		}
	}
	
	public class ViewHolder{
		TextView newsItem;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
//			exitWithConfirm();
			this.finish();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		Utils.releaseObject(info);
		Utils.releaseObject(btnGoHome);
		Utils.releaseObject(switchOnOff);
		BankcommApplication.getInstance().removeActivity(this);
	}
}
